#import "OmGetGoldVC.h"
#import "RechargeApiGetRechargePageDataModel.h"
#import "OmGetGoldVCTableViewCell.h"
#import "RootWebViewController.h"
#define CellReuseID @"OmGetGoldVCTableViewCell"
@interface OmGetGoldVC () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<RechargeApiGetRechargePageDataModel *> *modelArray;
@property (nonatomic, strong) UILabel *goldLabel;
@end
@implementation OmGetGoldVC
- (void)refreshSelf {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"recharge_api" A:@"get_recharge_page_data"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [CYNET POSTv2:url parameters:parameters responseCache:nil success:^(id responseObject) {
        NSLog(@"%@", responseObject);
        self.goldLabel.text = [responseObject[@"my_gold"] stringValue];
        self.modelArray = [RechargeApiGetRechargePageDataModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [self.tableView reloadData];
    } failure:^(NSString *error) {
    } hasCache:NO];
}
- (void)configSelf {
    [self setHiddenForTabBar:YES navigationBar:NO];
}
- (void)allocateComponents {
    self.backgroundImageView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"OmGetGoldVCBG")];
    [self.view addSubview:self.backgroundImageView];
    self.tableView = [[UITableView alloc] init]
    .fp_delegate(self)
    .fp_dataSource(self)
    .fp_separatorStyle(UITableViewCellSeparatorStyleNone)
    .fp_registerClassForCellReuseIdentifier([OmGetGoldVCTableViewCell class], CellReuseID)
    .fp_backgroundColor([UIColor clearColor]);
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    self.goldLabel = [[UILabel alloc] init].fp_font([UIFont systemFontOfSize:RPX(31) weight:UIFontWeightMedium]).fp_textColor([UIColor whiteColor]);
    [self.view addSubview:self.goldLabel];
}
- (void)layoutComponents {
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.backgroundImageView.mas_width).multipliedBy(16.0/9);
        make.left.right.bottom.equalTo(self.view);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(WINDOW_WIDTH));
        make.height.equalTo(@(RPX(360)));
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(RPX(-95));
    }];
    [self.goldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(RPX(-486));
        make.left.equalTo(self.view).offset(RPX(20));
    }];
}
#pragma mark - TableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[HUDHelper sharedInstance] loading:ASLocalizedString(@"请求中..")];
    RechargeApiGetRechargePageDataModel *model = self.modelArray[indexPath.row];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"pay_api" A:@"pay_gold"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"rid"] = model.ID;
    [CYNET POST:url parameters:parameters responseCache:nil success:^(id responseObject) {
        [MBProgressHUD hideHUD];
        RootWebViewController *webView = [[RootWebViewController alloc] init];
        webView.url = responseObject;
        [self.navigationController pushViewController:webView animated:YES];
    } failure:^(NSString *error) {
        [MBProgressHUD hideHUD];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD showInfoMessage:error];
        });
    } hasCache:NO];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RechargeApiGetRechargePageDataModel *model = self.modelArray[indexPath.row];
    OmGetGoldVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseID];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return RPX(60);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}
@end
